#include "init.h"

void init(char *title)
{
    if(SDL_Init(SDL_INIT_VIDEO ) < 0)
    {
        printf("Could not initialize SDL: %s\n", SDL_GetError());
        exit(1);
    }

    jeu.screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 0,SDL_HWPALETTE|SDL_DOUBLEBUF);

    if (jeu.screen == NULL)
    {
        printf("Couldn't set screen mode to %d x %d: %s\n", SCREEN_WIDTH, SCREEN_HEIGHT, SDL_GetError());
        exit(1);
    }

    SDL_WM_SetCaption(title, NULL);

    SDL_ShowCursor(SDL_DISABLE);

    if (TTF_Init() < 0)
    {
        printf("Couldn't initialize SDL TTF: %s\n", SDL_GetError());

        exit(1);
    }

    font = loadFont("GenBasB.ttf", 32);

    jeu.continuer = 1;

    startMenu.nbrBouton = 2;
}


void delay(unsigned int frameLimit)
{
    unsigned int ticks = SDL_GetTicks();

    if (frameLimit < ticks)
    {
        return;
    }

    if (frameLimit > ticks + 16)
    {
        SDL_Delay(16);
    }
    else
    {
        SDL_Delay(frameLimit - ticks);
    }
}


void loadGame(void)
{
    int i;

    map.background = loadImage("map1.bmp");
    map.startX = 0;
    map.startY = 0;
    map.maxX = map.background->w;

    player.res[LEFT] = loadImage("mouv22.png");
    player.res[RIGHT] = loadImage("mouv111.png");

    jeu.nombreMonstres = 0;
    for(i=0;i<20;i++)
    {
        initializeMonster(i*400+400,450);
    }

    jeu.vies = 5;
    jeu.score = 0;
    jeu.timeBegin = SDL_GetTicks();
    jeu.time = 120;

    jeu.HUD_vie = loadImage("life.png");
    jeu.HUD_etoiles = loadImage("stars.png");
    jeu.Shuriken_image = loadImage("Boule_de_feu.png");
    jeu.gameOver = loadImage("over.png");
}

void initializePlayer(void)
{
    player.sprite = player.res[RIGHT];
    player.x = 20;
    player.y = 500 - PLAYER_HEIGTH;
    
    player.direction = RIGHT;
    player.etat = IDLE;
   
    player.frameNumber = 0;
    player.frameTimer = TIME_BETWEEN_2_FRAMES;

    player.w = PLAYER_WIDTH;
    player.h = PLAYER_HEIGTH;

    player.timerMort = 0;
    player.onGround = 0;
    jeu.nombreFireballs = 0;
}


void cleanup()
{
    int i;

    if(map.background != NULL)
    {
        SDL_FreeSurface(map.background);
    }
    if (jeu.HUD_etoiles != NULL)
    {
        SDL_FreeSurface(jeu.HUD_etoiles);
    }
    if (jeu.HUD_vie != NULL)
    {
        SDL_FreeSurface(jeu.HUD_vie);
    }
    if (jeu.Shuriken_image != NULL)
    {
        SDL_FreeSurface(jeu.Shuriken_image);
    }

    if (player.sprite != NULL)
    {
        SDL_FreeSurface(player.sprite);
        SDL_FreeSurface(player.res[0]);
        SDL_FreeSurface(player.res[1]);
    }

    for(i = 0 ; i < MONSTER_MAX ; i++)
    {
        if (mob[i].sprite != NULL)
        {
            SDL_FreeSurface(mob[i].sprite);
            SDL_FreeSurface(mob[i].res[0]);
            SDL_FreeSurface(mob[i].res[1]);
        }
    }

    if(jeu.gameOver != NULL)
    {
        SDL_FreeSurface(jeu.gameOver);
    }

    for(i=0;i<startMenu.nbrBouton;i++)
    {
        if(startMenu.option[i] != NULL)
        {
            SDL_FreeSurface(startMenu.option[i]);
        }
        if(startMenu.optionSelected[i] != NULL)
        {
            SDL_FreeSurface(startMenu.optionSelected[i]);
        }
    }

    if(startMenu.background != NULL)
    {
        SDL_FreeSurface(startMenu.background);
    }

    closeFont(font);

    TTF_Quit();
    SDL_Quit();
    exit(0);
}


void initializeMonster(int x, int y)
{
    if (jeu.nombreMonstres < MONSTER_MAX )
    {
        mob[jeu.nombreMonstres].res[LEFT] = loadImage("zombie1.png");
        mob[jeu.nombreMonstres].res[RIGHT] = loadImage("zombie2.png");

        mob[jeu.nombreMonstres].sprite = mob[jeu.nombreMonstres].res[LEFT];

        mob[jeu.nombreMonstres].direction = LEFT;
        mob[jeu.nombreMonstres].etat = WALK;

        mob[jeu.nombreMonstres].frameNumber = 0;
        mob[jeu.nombreMonstres].frameTimer = TIME_BETWEEN_2_FRAMES;

        mob[jeu.nombreMonstres].x = x;
        mob[jeu.nombreMonstres].y = y;

        mob[jeu.nombreMonstres].w = 50;
        mob[jeu.nombreMonstres].h = 100;

        mob[jeu.nombreMonstres].timerMort = 0;
        mob[jeu.nombreMonstres].onGround = 0;
        
        jeu.nombreMonstres++;     
    }
}


void createFireBall(void)
{
    if (jeu.nombreFireballs < FIREBALLS_MAX)
    {
        shuriken[jeu.nombreFireballs].w = jeu.Shuriken_image->w;
        shuriken[jeu.nombreFireballs].h = jeu.Shuriken_image->h;

        if ( player.direction == RIGHT )
        {
            shuriken[jeu.nombreFireballs].x = player.x + 15;
            shuriken[jeu.nombreFireballs].y = player.y + 15;
            shuriken[jeu.nombreFireballs].direction = RIGHT;
        }
        else
        {
            shuriken[jeu.nombreFireballs].x = player.x - 15;
            shuriken[jeu.nombreFireballs].y = player.y + 15;
            shuriken[jeu.nombreFireballs].direction = LEFT;
        }
        jeu.nombreFireballs++;
    } 
}